package com.platform.modules.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.common.enumstatus.SystemEnum;
import com.platform.modules.sys.entity.SysOrgAptitudeEntity;
import com.platform.modules.sys.entity.SysOrgUserEntity;
import com.platform.modules.sys.entity.SysUserAptitudeEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysExamineStatusService;
import com.platform.modules.sys.service.SysOrgAptitudeService;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysOrgUserService;
import com.platform.modules.sys.service.SysUserAptitudeService;
import com.platform.modules.sys.service.SysUserService;


@Api(tags = "AuditContoller|监管单位审核管理")
@RestController
@RequestMapping("audit")
public class AuditContoller extends AbstractController{
	
	@Autowired
	private SysOrgService sysOrgService;
	@Autowired
	private SysOrgUserService sysOrgUserService;
	@Autowired
	private SysUserAptitudeService sysUserAptitudeService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysExamineStatusService sysExamineStatusService;
	@Autowired	
	private SysOrgAptitudeService sysOrgAptitudeService;
	@Value("${spring.mail.emailTile}")
	private String emailTile;
	
	@Value("${spring.mail.emailParameter}")
	private String emailParameter;

	@Value("${spring.mail.tgEmailParameter}")
	private String tgEmailParameter;
	
	@Value("${spring.mail.btgEmailParameter}")
	private String btgEmailParameter;
	
	
	
	@ApiOperation(value = "审核企业（通过）", notes = "审核企业（通过）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "orgNo", value = "机构id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditor", value = "审核人", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditTxt", value = "审核意见", dataType = "String")
    })
	@SysLog("审核企业（通过）")
	@PostMapping("/passOrg")
	public RestResponse passOrg(String orgNo,String auditor,String auditTxt) {
		
		if(StringUtils.isBlank(orgNo)){
			return RestResponse.error("机构orgNo不能为空");
		}
    	
    	SysUserEntity  user = this.getUser();
    	String auditOrgNo = user.getOrgNo();  //监管机构Id
//    	if(user != null && user.getUserId() != null){
//    		SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
// 			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
// 				Map<String, Object> params = new HashMap<String, Object>();
// 					params.put("userId", sysUserEntity.getUserId());
// 				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
// 				if(orgUserList !=null && orgUserList.size()>0){
// 					auditOrgNo = orgUserList.get(0).getOrgNo();
// 				}
// 			}
// 		}
    	String result = sysExamineStatusService.auditOrg(orgNo, auditOrgNo, auditor, auditTxt, user,"2");
    	if("noOrg".equals(result)){
    		return RestResponse.error("无此单位，请检查单位orgNo是否正确");
    	}else if("SUCCESS".equals(result)){
    		return RestResponse.success();
    	}else if("noEmail".equals(result)){
    		return RestResponse.error("此单位邮箱有误");
    	}
    	return RestResponse.success();
		
	}
	
	/**
	 * 审核企业（不通过）  
	 */
	@ApiOperation(value = "审核企业（不通过）", notes = "审核企业（不通过）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "orgNo", value = "机构id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditor", value = "审核人", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditTxt", value = "审核意见", dataType = "String")
    })
	@SysLog("审核企业（不通过）")
	@PostMapping("/noPassOrg")
	public RestResponse noPassOrg(String orgNo,String auditor,String auditTxt) {
		
		if(StringUtils.isBlank(orgNo)){
			return RestResponse.error("机构orgNo不能为空");
		}
	
	    	String auditOrgNo = "";
	    	 SysUserEntity  user = this.getUser();
	    	 if(user != null && user.getUserId() != null){
	 			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
	 			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
	 				Map<String, Object> params = new HashMap<String, Object>();
	 					params.put("userId", sysUserEntity.getUserId());
	 				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
	 				if(orgUserList !=null && orgUserList.size()>0){
	 					auditOrgNo = orgUserList.get(0).getOrgNo();
	 				}
	 			}
	 		}
	    	
	    String result = sysExamineStatusService.auditOrg(orgNo,auditOrgNo,auditor, auditTxt,user,"4");
		if("noOrg".equals(result)){
			return RestResponse.error("无此单位，请检查单位Id--orgNo是否正确");
		}else if("SUCCESS".equals(result)){
		    return RestResponse.success();
		}else if("noEmail".equals(result)){
	    	  return RestResponse.error("此单位邮箱有误");
	      }
		return RestResponse.success();

	}
	
	
	/**
	 * 查询审核企业列表
	 */
	@ApiOperation(value = "查询审核企业列表", notes = "查询审核企业列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "status", value = "企业审核状态", dataType = "Integer")
    })
	@SysLog("查询审核企业列表")
	@PostMapping("/auditOrgList")
	public RestResponse auditOrgList(Integer currPage,Integer limit,String status) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", PageUtil.getCurrPage(currPage));
		params.put("limit", PageUtil.getLimit(limit));
		params.put("status", status);
		IPage page = sysOrgService.queryPage(params);
		return RestResponse.success().put("page", page);
	}
	
	/**
	 * 企业资质信息列表（已退回）
	 * 0 待提交 1 审核中 2 已通过 3 被退回 4 已失效
	 * 见字典
	 * APTITUDE_STATUS
	 */
	@ApiOperation(value = "企业资质信息列表（已退回）", notes = "企业资质信息列表（已退回）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer")
    })
	@SysLog("企业资质信息列表（已退回）")
	@PostMapping("/noPassList")
	public RestResponse noPassList(Integer currPage,Integer limit) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", PageUtil.getCurrPage(currPage));
		params.put("limit", PageUtil.getLimit(limit));
		params.put("aptitudeStatus", "3");  //审核不通过
//		List<SysOrgAptitudeEntity> orgAptitudeList = sysOrgAptitudeService.queryAll(params);
		Page page = sysOrgAptitudeService.queryAllPage(params);
		return RestResponse.success().put("page", page);
	}
	
	/**
	 * 企业资质信息列表（已通过）
	 * 0 待提交 1 审核中 2 已通过 3 被退回 4 已失效
	 * 见字典
	 * APTITUDE_STATUS
	 */
	@ApiOperation(value = "企业资质信息列表（已通过）", notes = "企业资质信息列表（已通过）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer")
    })
	@SysLog("企业资质信息列表（已通过）")
	@PostMapping("/passList")
	public RestResponse passList(Integer currPage,Integer limit) {
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("aptitudeStatus", "2");
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
		Page page = sysOrgAptitudeService.queryAllPage(params);
		return RestResponse.success().put("page", page);
	}
	
	/**
	 * 资质信息列表（审核中）
      0 待提交 1 审核中 2 已通过 3 被退回 4 已失效
	 * 见字典
	 * APTITUDE_STATUS
	 */
	@ApiOperation(value = "企业资质信息列表（待审核）", notes = "企业资质信息列表（待审核）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer")
    })
	@SysLog("企业资质信息列表（待审核）")
	@PostMapping("/dShenList")
	public RestResponse dShenList(Integer currPage,Integer limit) {
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("nPass", "1");
		Page page = sysOrgAptitudeService.queryAllPage(params);
		return RestResponse.success().put("page", page);
	}
	
	/**
	 * 审核企业资质信息（通过）
	 * 0 待提交 1 审核中 2 已通过 3 被退回 4 已失效
	 * 见字典
	 * APTITUDE_STATUS
	 */
	@ApiOperation(value = "审核企业资质信息（通过）", notes = "审核企业资质信息（通过）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "aptitudeId", value = "资质id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditor", value = "审核人", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditTxt", value = "审核意见", dataType = "String")
    })
	@SysLog("审核企业资质信息（通过）")
	@PostMapping("/passOrgAptitude")
	public RestResponse passOrgAptitude(String aptitudeId,String auditor,String auditTxt) {
		
		SysOrgAptitudeEntity sysOrgAptitudeEntity = sysOrgAptitudeService.getById(aptitudeId);
	    if(sysOrgAptitudeEntity != null){
	    	//获取当前登陆审核单位Id
	    	String auditOrgNo = "";
	    	//获取审核单位人员信息
	    	 SysUserEntity  user = this.getUser();
	    	 if(user != null && user.getUserId() != null){
	 			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
	 			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
	 				Map<String, Object> params = new HashMap<String, Object>();
	 					params.put("userId", sysUserEntity.getUserId());
	 				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
	 				if(orgUserList !=null && orgUserList.size()>0){
	 					auditOrgNo = orgUserList.get(0).getOrgNo();
	 				}
	 			 }
	 		  }
	    	 sysOrgAptitudeService.passOrgAptitude(sysOrgAptitudeEntity, auditTxt, auditor, auditOrgNo);
	    }

		return RestResponse.success();
	}
	
	/**
	 * 审核企业资质信息（不通过）
     * 0 待提交 1 审核中 2 已通过 3 被退回 4 已失效
	 * 见字典
	 * APTITUDE_STATUS
	 */
	@ApiOperation(value = "审核企业资质信息（不通过）", notes = "审核企业资质信息（不通过）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "aptitudeId", value = "单位资质id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditor", value = "审核人", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "auditTxt", value = "审核意见", dataType = "String")
    })
	@SysLog("审核企业资质信息（不通过）")
	@PostMapping("/noPassOrgAptitude")
	public RestResponse noPassOrgAptitude(String aptitudeId,String auditor,String auditTxt) {
		
		SysOrgAptitudeEntity sysOrgAptitudeEntity = sysOrgAptitudeService.getById(aptitudeId);
	    if(sysOrgAptitudeEntity != null){
	    	String auditOrgNo = "";
	    	 SysUserEntity  user = this.getUser();
	    	 if(user != null && user.getUserId() != null){
	 			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
	 			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
	 				Map<String, Object> params = new HashMap<String, Object>();
	 					params.put("userId", sysUserEntity.getUserId());
	 				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
	 				if(orgUserList !=null && orgUserList.size()>0){
	 					auditOrgNo = orgUserList.get(0).getOrgNo();
	 				}
	 			}
	 		}
	    	 sysOrgAptitudeService.noPassOrgAptitude(sysOrgAptitudeEntity, auditTxt, auditor, auditOrgNo);

	    }
		return RestResponse.success();
	}
	
	/**
	 * 企业资质列表-本单位-前端使用
	 * 0 待提交 1 审核中 2 已通过 3 被退回 4 已失效
	 * 见字典
	 * APTITUDE_STATUS
	 */
	@ApiOperation(value = "企业资质列表-本单位", notes = "企业资质列表-本单位")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "orgNo", value = "企业id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "aptitudeStatus", value = "企业资质状态", dataType = "String")
    })
	@SysLog("企业资质列表")
	@PostMapping("/orgAptitudeList")
	public RestResponse orgAptitudeList(Integer currPage,Integer limit,String orgNo,String aptitudeStatus) {
		
		SysUserEntity  user = this.getUser();
		if(user != null && user.getUserId() != null){
			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", sysUserEntity.getUserId());
				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
				if(orgUserList !=null && orgUserList.size()>0){
					orgNo = orgUserList.get(0).getOrgNo();         //获取当前登陆企业账号所属单位id
				}
			}
		}else{
			return RestResponse.error("请登录企业账号");
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("orgNo", orgNo);
			params.put("aptitudeStatus", aptitudeStatus);
			params.put("yTiJiaoAptitudeStatus", "yTiJiaoAptitudeStatus");

		Page page = sysOrgAptitudeService.queryPage(params);
		return RestResponse.success().put("page", page);
	}
	

	/**
	 * 审核人员资质（通过）
	 * 0待提交1.审核中2有效3被退回  
	 * 见字典 USER_APTITUDE_STATUS
	 */
	@ApiOperation(value = "审核人员资质（通过）", notes = "审核人员资质（通过）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userAptitudeId", value = "人员资质Id", dataType = "String")
    })
	@SysLog("审核人员资质（通过）")
	@PostMapping("/passUserAptitude")
	public RestResponse pssUserAptitude(String userAptitudeId) {
		if(StringUtils.isBlank(userAptitudeId)){
			return RestResponse.error("人员资质Id不能为空，请重新输入！");
		}
		SysUserAptitudeEntity sysUserAptitudeEntity = sysUserAptitudeService.getById(userAptitudeId);
		if(sysUserAptitudeEntity != null){
			String auditOrgNo = "";
	    	 SysUserEntity  user = this.getUser();
	    	 if(user != null && user.getUserId() != null){
	 			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
	 			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
	 				Map<String, Object> params = new HashMap<String, Object>();
	 					params.put("userId", sysUserEntity.getUserId());
	 				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
	 				if(orgUserList !=null && orgUserList.size()>0){
	 					auditOrgNo = orgUserList.get(0).getOrgNo();
	 				}
	 			}
	 		}else{
	 			return RestResponse.error("此账号非企业账号，请登录企业账号！");
	 		}
	     sysUserAptitudeService.auditUserAptitude(userAptitudeId, auditOrgNo,AptitudeEnum.aptitudeStatus.YBSYPZ.getKey(), sysUserAptitudeEntity); 
		}else{
			return RestResponse.error("此人员资质信息有误请重新输入人员资质Id！");
		}
		return RestResponse.success();
	}
	
	/**
	 * 审核人员资质（不通过）
	 * 0待提交1.审核中2有效3被退回  
	 * 见字典 USER_APTITUDE_STATUS
	 */
	@ApiOperation(value = "审核人员资质（不通过）", notes = "审核人员资质（通过）")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "userAptitudeId", value = "人员资质id", dataType = "String")
    })
	@SysLog("审核人员资质（不通过）")
	@PostMapping("/noPassUserAptitude")
	public RestResponse noPassUserAptitude(String userAptitudeId) {
		
		if(StringUtils.isBlank(userAptitudeId)){
			return RestResponse.error("人员资质Id不能为空，请重新输入！");
		}
		
		SysUserAptitudeEntity sysUserAptitudeEntity = sysUserAptitudeService.getById(userAptitudeId);
		if(sysUserAptitudeEntity != null){
			String auditOrgNo = "";
	    	 SysUserEntity  user = this.getUser();
	    	 if(user != null && user.getUserId() != null){
	 			SysUserEntity sysUserEntity = sysUserService.getById(user.getUserId());
	 			if(sysUserEntity != null && SystemEnum.accountType.ORG.getKey().equals(sysUserEntity.getAccountType())){
	 				Map<String, Object> params = new HashMap<String, Object>();
	 					params.put("userId", sysUserEntity.getUserId());
	 				List<SysOrgUserEntity> orgUserList = sysOrgUserService.queryAll(params);
	 				if(orgUserList !=null && orgUserList.size()>0){
	 					auditOrgNo = orgUserList.get(0).getOrgNo();
	 				}
	 			}
	 		}else{
	 			return RestResponse.error("此账号非企业账号，请登录企业账号！");
	 		}
	    	 
	    	 sysUserAptitudeService.auditUserAptitude(userAptitudeId, auditOrgNo,"3", sysUserAptitudeEntity); 
		}else{
			return RestResponse.error("此人员资质信息有误请重新输入人员资质Id！");
		}
		return RestResponse.success();
	}
	
	
	/**
	 * 人员资质列表
	 */
	@ApiOperation(value = "人员资质列表-根据userId查询", notes = "人员资质列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "userId", value = "人员id", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "aptitudeStatus", value = "人员资质状态", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "realName", value = "人员姓名", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "人员身份证号码", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "aptitudeNumber", value = "证书编号", dataType = "String")
    })
	@SysLog("人员资质列表")
	@PostMapping("/userQualiList")
	public RestResponse userQualiList(Integer currPage,Integer limit,String userId,String aptitudeStatus,String realName,String userCard,String aptitudeNumber) {
		
		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("userId", userId);
			params.put("realName", realName);
			params.put("userCard", userCard);
			params.put("aptitudeNumber", aptitudeNumber);
			params.put("aptitudeStatus", aptitudeStatus);
		Page page = sysUserAptitudeService.queryPage(params);
		return RestResponse.success().put("page", page);
	}
	
	
	/**
	 * 监管机构查看业务人员列表
	 */
	@ApiOperation(value = "监管机构查看业务人员列表", notes = "监管机构查看业务人员列表")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "currPage", value = "当前页数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "limit", value = "分页步数", dataType = "Integer"),
		@ApiImplicitParam(paramType = "query", name = "userCard", value = "人员身份证号", dataType = "String")
    })
	@SysLog("监管机构查看业务人员列表")
	@PostMapping("/salesList")
	public RestResponse salesList(Integer currPage,Integer limit,String userCard) {

		Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", PageUtil.getCurrPage(currPage));
			params.put("limit", PageUtil.getLimit(limit));
			params.put("userCard", userCard);
		Page page = sysOrgAptitudeService.queryPage(params);

		return RestResponse.success().put("page", page);
	}
	
	
}
